#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/select.h>
#include <string.h>
#include "const.h"
#include "argSeparation.h"

int read_and_parsen (FILE *stream , char *** retstring , int *retnum)
{
	char buffer [ MAXSIZE ];
	char * pt ;
	char * argv [ MAXARG ];
	int i , argc ;

	if ( fgets ( buffer , MAXSIZE , stream )== NULL ) {
		if ( feof ( stream )) {
			fprintf ( stderr ," Fin de stream atteinte !\n");
			return P_ERROR ;
		} else if ( ferror ( stream )) {
			fprintf ( stderr ," Erreur sur stream !\n");
			return P_ERROR ;
		}
	}

	/* Eliminer le CR de fin de chaine */
	buffer [ strlen ( buffer )-1] ='\0';

	/* Traiter le cas sans argument */
	if ( strlen ( buffer ) == 0) {
		*retstring = NULL ;
		*retnum = 0;
		return RET_SUCCESS ;
	}

	/* Lire les arguments */
	argc = 0;
	if (( argv [ argc ++] = strtok_r ( buffer ,"\t " ,&pt )) == NULL ) {
		return P_ERROR ;
	}

	while (( argv [ argc ] = strtok_r ( NULL ,"\t " ,&pt )) != NULL ) {
		if (++ argc>= MAXARG ) break ;
	}

	/* Allour le tableau de retour */
	if ((* retstring = malloc ( argc * sizeof ( char *))) == NULL )
		return P_ERROR ;

	for ( i =0; i < argc ; i ++) {
		(* retstring )[ i ] = malloc ( MAXSIZE * sizeof ( char ));
		if ((*retstring )[i] == NULL ) {
			for (--i ;i >=0; i--) free ((* retstring )[i]);
			free (*retstring);
			return P_ERROR;
		}
	}

	/* Recopier les arguments */
	for ( i =0; i < argc ; i ++) {
	    strncpy ((* retstring )[ i ] , argv [ i ] , MAXSIZE );
	}
	* retnum = argc ;
	return ( P_SUCCESS );
}


/**
 * This code was retrieved from a web page,
 * originally it was conceived by Bjorn Reese:
 * www.kegel.com/dkftpbench/nonblocking.html
 */
int setNonBlocking(int fd){
	int flags;
#ifdef O_NONBLOCK
		if(-1 == (flags = fcntl(fd, F_GETFL, 0)))
			flags = 0;
		return fcntl(fd,F_SETFL, flags | O_NONBLOCK);
#else
		flags = 1;
		return ioctl(fd,FIOBIO,&flags);
#endif
}

int read_and_parse ( FILE * stream , char * retbuf )
{
	char buffer [ MAXSIZE ];
	setNonBlocking(fileno(stream));
	if ( fgets ( buffer , MAXSIZE , stream )== NULL ) {
		if ( feof ( stream )) {
			logMessage( stderr ,"Fin de stream atteinte! fileno: %d \n",fileno(stream));
			return ARG_FIM_DE_STREAM;
		} else if ( ferror ( stream )) {
			/* fprintf ( stderr ," Erreur sur stream !\n"); */
			return ARG_END_STREAM | ARG_ERR_STREAM;
		}
		return ARG_END_STREAM;

	}
	/* Eliminer le CR de fin de chaine */
	buffer [ strlen ( buffer ) -1] ='\0';
	sprintf ( retbuf ,"%s" , buffer );
	return strlen(retbuf);
}


void parseArgAndDupFiles(int argc,char** argv,int mask, FILE* in, FILE* out){
	int fd;
	int result;
	int filen;

	if(argc < 2)
		return;
	if(argc == 2){
	}
	if(mask & DUP_IN){
		fd = atoi(argv[1]);
		if(in == NULL){
			filen = STDIN_FILENO;
		}else{
			filen = fileno(in);
		}
		if(fd != filen){
			result = dup2(fd,filen);
			close(fd);
		}
		if(result == -1){
			logMessage(stderr,"could not init file %d\n",fd);
			sleep(2);
			exit(EXIT_FAILURE);
		}
	}

	
	if(mask & DUP_OUT){
		if(argc > 2){
			fd = atoi(argv[2]);
		}else{
			fd = atoi(argv[1]);
		}

		if(out == NULL){
			filen = STDOUT_FILENO;
		}else{
			filen = fileno(out);
		}
		if(fd != filen){
			result = dup2(fd,filen);
			close(fd);
		}
		if(result == -1){
			logMessage(stderr,"Could not init file %d\n",fd);
			sleep(2);
			exit(EXIT_FAILURE);
		}
	}
}

